# encoding: utf-8
# Datetime  : 2020/6/2 9:41 PM
# User      : zzz
# File      : serial_util.py

# sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from pathlib import Path
from pickle import *

from util import file_util

project_path = file_util.get_project_path()


def save_data(data):
    p = Path(f'{project_path}/data/process_log')
    if not p.parent.exists():  # 创建文件的上级目录
        p.parent.mkdir(parents=True)
    with open(p, 'wb') as f:  # 进行序列化，必须是二进制的输入，否则会报错
        dump(data, f)


def load_data():
    p = Path(f'{project_path}/data/process_log')
    if p.exists():
        with open(p, 'rb') as f:  # 此处对应的是二进制的输出
            data = load(f)
            # print(f"data: {data}")
            return data
    else:
        return []


def append_data(data):
    list = load_data()
    list.append(data)
    save_data(list)


def is_processed(url):
    processed_url_list = load_data()
    return url in processed_url_list


if __name__ == '__main__':
    data = ['zzz', 'aaa']
    save_data(data)
    append_data('test')
    print(load_data())
    append_data("lgege")
    print(load_data())
